package com.wql.rabbitmq;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class ChannelFactory {

	public static Channel getChannel(String host) {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost(host);
		Connection connection;
		try {
			connection = factory.newConnection();
			return connection.createChannel();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Channel getChannel() {
		return getChannel(Constant.IP);
	}

	public static void close(Closeable... cls) {
		for (Closeable cl : cls) {
			if (null != cl) {
				try {
					cl.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
